package com.oncecloud.devops.client.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.oncecloud.devops.client.dao.RoleActionMapper;
import com.oncecloud.devops.client.dao.RoleMapper;
import com.oncecloud.devops.client.dao.UserRoleMapper;
import com.oncecloud.devops.client.entity.Role;
import com.oncecloud.devops.client.entity.RoleAction;
import com.oncecloud.devops.client.entity.User;
import com.oncecloud.devops.client.entity.UserRoleEnv;
import com.oncecloud.devops.client.service.RoleService;

/**
 * 角色相关服务实现类
 * @author xzg
 *
 */
@Service
public class RoleServiceImp implements RoleService{

	@Resource
	private RoleMapper roleMapper;
	
	@Resource
	private UserRoleMapper userRoleMapper;
	
	@Resource
	private RoleActionMapper roleActionMapper;
	@Override
	public List<Role> getRolePermmitMod(User user) {
		// TODO Auto-generated method stub
		return roleMapper.getRolePermmitMod(user);
	}
	/**
	 * 增加角色
	 * @throws SQLException 
	 * @see com.oncecloud.devops.client.service.RoleService#addRole(com.oncecloud.devops.client.entity.Role)
	 */
	@Override
	public void addRole(Role role) {
		// TODO Auto-generated method stub
		
			try {
				roleMapper.insertRole(role);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}
	/** 批量增加用户角色
	 * @see com.oncecloud.devops.client.service.RoleService#addUserRole(java.util.List)
	 */
	@Override
	public int addUserRole(List<UserRoleEnv> ure) {
		// TODO Auto-generated method stub
		try {
			return userRoleMapper.insertListURE(ure);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 批量增加角色权限
	 * @see com.oncecloud.devops.client.service.RoleService#addRoleAuthority(java.util.List)
	 */
	@Override
	public int addRoleAuthority(List<RoleAction> roleActions) {
		// TODO Auto-generated method stub
		 try {
			return roleActionMapper.insertListRA(roleActions);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 return 0;
		}
		
	}
	
	@Override
	public int updateRoleById(Role role) throws SQLException {
		// TODO Auto-generated method stub
		return roleMapper.updateRoleById(role);
	}
	
	/**
	 * @see com.oncecloud.devops.client.service.RoleService#deleteRoleAction(java.util.List)
	 */
	@Override
	public int deleteRoleAction(List<Integer> list) throws SQLException {
		// TODO Auto-generated method stub
		 try {
			 return roleActionMapper.deleteRoleActions(list);
			 
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				 return 0;
			}
	}

	
}
